package de.streblow.inventur;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class InputActivity extends Activity {

	public String teamID;
	public FileOutputStream fosInventurfile;
	public Boolean bolInventurfileOpen;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_input);
		
		teamID = getIntent().getExtras().getString("teamID");
		bolInventurfileOpen = false;
		try {
			fosInventurfile = openFileOutput(teamID, Context.MODE_APPEND);
			bolInventurfileOpen = true;
		} catch (FileNotFoundException e) {
			Messagebox.showMessagebox(InputActivity.this, "Fehler!", e.getMessage());
		}

		final ImageButton button1 = (ImageButton) findViewById(R.id.imageButton1);
		button1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(InputActivity.this, ScanActivity.class);
				startActivityForResult(intent, 0);
			}
		});

		final ImageButton button2 = (ImageButton) findViewById(R.id.imageButton2);
		button2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(InputActivity.this, ScanActivity.class);
				startActivityForResult(intent, 1);
			}
		});

		final Button button3 = (Button) findViewById(R.id.buttonAddItem);
		button3.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				String strDaten = "";
				TextView tvArtikelnr = (TextView) findViewById(R.id.editArtikelnr);
				strDaten = strDaten + tvArtikelnr.getText().toString();
				TextView tvLagerplatz = (TextView) findViewById(R.id.editLagerplatz);
				strDaten = strDaten + ";" + tvLagerplatz.getText().toString();
				TextView tvMenge = (TextView) findViewById(R.id.editMenge);
				strDaten = strDaten + ";" + tvMenge.getText().toString();
				Spinner spEinheit = (Spinner) findViewById(R.id.spinnerEinheit);
				strDaten = strDaten + ";" + spEinheit.getSelectedItem().toString() + "\n";
				if (tvArtikelnr.getText().toString().isEmpty() |
						tvLagerplatz.getText().toString().isEmpty() |
						tvMenge.getText().toString().isEmpty() ) {
					return;
				}
				if (!bolInventurfileOpen) {
					try {
						fosInventurfile = openFileOutput(teamID, Context.MODE_APPEND);
						bolInventurfileOpen = true;
					} catch (FileNotFoundException e) {
						Messagebox.showMessagebox(InputActivity.this, "Fehler!", e.getMessage());
					}
				}
				try {
					fosInventurfile.write(strDaten.getBytes());
				} catch (IOException e) {
					Messagebox.showMessagebox(InputActivity.this, "Fehler!", e.getMessage());
				}
				tvMenge.setText("");
				tvMenge.setFocusableInTouchMode(true);
				tvMenge.requestFocus();
			}
		});

		final Button button4 = (Button) findViewById(R.id.buttonShowEntries);
		button4.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					fosInventurfile.close();
				} catch (IOException e) {
					// already closed
					e.printStackTrace();
				}
				bolInventurfileOpen = false;
				Intent intent = new Intent(InputActivity.this, TableActivity.class);
				intent.putExtra("teamID", teamID);
				startActivity(intent);
			}
		});

		TextView tvArtikelnr = (TextView) findViewById(R.id.editArtikelnr);
		TextView tvLagerplatz = (TextView) findViewById(R.id.editLagerplatz);
		TextView tvMenge = (TextView) findViewById(R.id.editMenge);
		Spinner spEinheit = (Spinner) findViewById(R.id.spinnerEinheit);
		tvArtikelnr.setText("");
		tvLagerplatz.setText("");
		tvMenge.setText("");
		spEinheit.setSelection(0);
		button1.setFocusableInTouchMode(true);
		button1.requestFocus();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.action_settings:
			Intent intent = new Intent(InputActivity.this, OptionsActivity.class);
			startActivityForResult(intent, 0);
			return true;
		case R.id.action_help:
			HelpDialog help = new HelpDialog(this);
			help.setTitle(R.string.help_title);
			help.show();
			return true;
		case R.id.action_about:
			AboutDialog about = new AboutDialog(this);
			about.setTitle(R.string.about_title);
			about.show();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		TextView tvArtikelnr = (TextView) findViewById(R.id.editArtikelnr);
		TextView tvLagerplatz = (TextView) findViewById(R.id.editLagerplatz);
		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {
				String contents = intent.getStringExtra("SCAN_RESULT");
				tvArtikelnr.setText(contents);
				// Handle successful scan
			} else if (resultCode == RESULT_CANCELED) {
				// Handle cancel
			}
		} else if (requestCode == 1) {
			if (resultCode == RESULT_OK) {
				String contents = intent.getStringExtra("SCAN_RESULT");
				tvLagerplatz.setText(contents);
				// Handle successful scan
			} else if (resultCode == RESULT_CANCELED) {
				// Handle cancel
			}
		}
	}

}
